package com.realife.eson.common.util.common;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;

import java.beans.PropertyDescriptor;
import java.util.Arrays;
import java.util.List;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2022/3/30 15:52
 * @description:
 */

public class MyBeanUtils extends BeanUtils {

    /**
     * 只复制某些属性
     *
     * @param source
     * @param target
     * @param includeProperties
     * @throws BeansException
     */
    public static void copyProperties(Object source, Object target, List<String> includeProperties) throws BeansException {
        String[] ignoreProperties = Arrays.stream(BeanUtils.getPropertyDescriptors(source.getClass()))
                .map(PropertyDescriptor::getName)
                .filter(name -> !includeProperties.contains(name))
                .toArray(String[]::new);
        copyProperties(source, target, ignoreProperties);
    }

}

